import fetch from '@system.fetch';
import prompt from '@system.prompt';
import geolocation from '@system.geolocation';
import http from '@ohos.net.http';
import router from '@system.router';
export default {
    data: {
        colors: [
            {
                "isClick":false,
                "title":"打卡签到",
                "color": "#ccddea",

            },
            {
                "isClick":false,
                "title":"健康打卡",
                "color": "#eeaacd",

            },
            {
                "isClick":false,
                "title":"运动打卡",
                "color": "#aaacca",

            },
            {
                "isClick":false,
                "title":"定时打卡",
                "color": "#bbbbbb"
            }
        ],
        initColor:"#ccaaff",
        activeCardColor:"white",
        "isClick":false,
        whiteColor:"white",
        list:[],
        id:'-1',
        location: { },
        modeFlag: "half",
        clockCode:'',
        topic:'',
        userClockCode:'',
        status:''

    },
    onInit() {
//        this.title = this.$t('strings.world');
//        for(var i = 0; i<10;i++){
//            var item = {uri:"pages/onePage/onePage",
//                title:"this is title"+i,id:"item_"+1}
//            this.list.push(item);
//        }

    },

        activeDetial(e){
            console.info("点击了1："+e.idx)
            console.info("点击了："+JSON.stringify(e))
            this.initColor=e.target.attr.protype.color
            this.activeCardColor=e.target.attr.protype.color
            for(let i = 0;i<this.colors.length;i++){
                if (i== e.target.id){
                    console.log("点击了aaa"+e.target.attr.id)
                    this.colors[e.target.attr.id].isClick = true
                    this.id= e.target.attr.id
                    if (this.id==0) {
                        let httpRequest = http.createHttp();
                        httpRequest.request(
                        // 填写http请求的url地址，可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
//                            "10.16.70.166:3000",
                             "https://www.baidu.com/",
                            {
                                method: 'GET', // 可选，默认为“GET”
                                // 开发者根据自身业务需要添加header字段
                                header: {
                                    'Content-Type': 'application/json'
                                },
                                // 当使用POST请求时此字段用于传递内容
                                extraData: "data to post",
                                readTimeout: 60000, // 可选，默认为60000ms
                                connectTimeout: 60000 // 可选，默认为60000ms
                            },(err, data) => {
                            if (!err) {
                                // data.result为http响应内容，可根据业务需要进行解析
                                console.info('Result:' + data.result);
                                console.info('code:' + data.responseCode);
                                // data.header为http响应头，可根据业务需要进行解析
                                console.info('header:' + data.header);
                            } else {
                                console.info('error:' + err.data);
                            }
                        }
                        );
                    }
                }else{
                    this.colors[i].isClick = false
                    console.log("每点击")
                }
            }
//            this.$app.$def.eventBus.emit(e)
        },
    goToPage(e){
        console.log("滑动事件:"+JSON.stringify(e))
        router.push({
            uri: "pages/onePage/onePage",
            params:{
                id:this.id,
                color:this.colors[this.id].color
            }
        })
        console.log("进入想起")
    },
    getLocation(){
        geolocation.getLocation({
            success: function(data) {
                console.log('success get location data. latitude:' + data.latitude);
                this.location.longitude = data.longitude
                this.location.latitude = data.latitude
            },
            fail: function(data, code) {
                console.log('fail to get location. code:' + code + ', data:' + data);
            },
        });
        //        geolocation.isLocationEnabled().then((result) => {
//            console.log('promise, isLocationEnabled: ' + result.code + " data: " + result.data);
            //如果位置服务开启，获取位置；否则提示开启位置
//            if(result.data){
//                var requestInfo = {'priority': 0x203, 'scenario': 0x300,'maxAccuracy': 0};
//                geolocation.getCurrentLocation().then((result) => {
//                    console.log('current location: ' + JSON.stringify(result.data));
//                    console.log("经纬度："+result.data.latitude)
                    //获取到的位置信息进行转换
//                    var reverseGeocodeRequest = {latitude: result.data.latitude, "longitude": result.data.longitude, "maxItems": 1};
//                    geolocation.getAddressesFromLocation(reverseGeocodeRequest, (err, data) => {
//                        console.log('getAddressesFromLocation: ' + err + " data: " + JSON.stringify(data));
//                        this.location = data
//                    });
//
//                });
//            }else{
//                prompt.showToast({message:"请开启手机位置服务"})
//            }
//        });
    },
    onSubmit(result) {
        console.log("这是位置信息："+JSON.stringify(this.location))

        //        console.log() // radio1 or radio2
        console.log(result.value.code) // text input value
        var Http = "http://120.78.198.170:3000/clockin?";
        let clockTime = "15.30"
        let username = "183"
        this.userClockCode = result.value.code
        fetch.fetch({
            url: Http+"code="+this.userClockCode+"&username="+username,
//            url: Http,
            responseType: 'json',
            headers: {
                'content-type': 'application/json'
            },
            method: 'GET',
            success: res => {
                console.info(res)
                console.info("我是pinglun列表：" + res.data)
                let aa = res.data;
                let bb = JSON.parse(aa)
                this.status = bb.data.message
                console.log("这是签到状态："+bb)
//                this.id = "-1"
            },
            fail:{
            }
        });
    },
    clockTopic(v){
        console.log("签到主题："+JSON.stringify(v))
        console.log("签到主题："+v.value)
        this.topic = v.value;
    },
    //点击事件事件队列
    ArrayQueue(){
        var arr = [];
        //入队操作
        this.push = function(element){
            arr.push(element);
            return true;
        }
        //出队操作
        this.pop = function(){
            return arr.shift();
        }
        //获取队首
        this.getFront = function(){
            return arr[0];
        }
        //获取队尾
        this.getRear = function(){
            return arr[arr.length - 1]
        }
        //清空队列
        this.clear = function(){
            arr = [];
        }
        //获取队长
        this.size = function(){
            return arr.length;
        }
    },
    //获取签到码
    getClockCode(){
//        let timeQueue = new this.ArrayQueue()
//        console.log("这是时间队列："+timeQueue)
//        //获取当前时间戳
//        timeQueue.push(Date.parse(new Date()))
//;
//        let preTime = timeQueue.pop()
//        let secTime = timeQueue.pop()
//        console.log("第一次时间："+preTime);
//        console.log("第二次时间："+secTime);
//
//        //时间差
//        let time_T = secTime - preTime
//        console.log("时间差"+time_T)
//        if(time_T%5000>=5|| time_T<0){//如果小于5分钟提示用户
            //获取随机数
            this.randomNum()
//        }else{
            //弹出对话框
//            this.$element('simpledialog').show()
            //取消
//            this.cancelSchedule();
            //确认
//            this.setSchedule();
//        }

    },
    randomNum(){
        var strArr = [0,1,2,3,4,5,6,7,8,9];
        var newArr = [];
        for(var j = 0; j<4;j++){
            var a = Math.floor(Math.random()*strArr.length);
            newArr.push(strArr[a])
        }
        console.log("随机数："+newArr.toString())
        let aa = newArr.toString()
        let bb = aa.replace(/\,/g,'');
        this.clockCode = bb;
        console.log("随机数2："+this.clockCode)
//        this.$element("clockTopic")
        //把生成随机数进行云端存储，
        //        Math.floor(Math.random()*())
    },
    cancelSchedule(e) {
        prompt.showToast({
            message: 'Successfully cancelled'
        })
        this.$element('simpledialog').close()

    },
    setSchedule(e) {
        this.$element('simpledialog').close()
        this.randomNum()
        prompt.showToast({
            message: 'Successfully confirmed'
        })
    },
    onReset() {
        console.log('reset all value')
        this.status=''
    },
    controlPanel(){
        this.$element('simplepanel').show()
    },
//    showPanel() {
//
//    },
    closePanel() {
        this.$element('simplepanel').close()
    },
    //发起签到
    confirmPanel(){
        if(this.topic==''){
            prompt.showToast({message:"主题不能为空"})
        }
        if (this.clockCode=='') {
            prompt.showToast({message:"签到码不能为空"})

        } else{
            console.log("进入请求发送主题签到码")
            var Http = "http://120.78.198.170:3000/clockCode?code=";
            fetch.fetch({
                //            url: Http+this.clockCode+"&"+clockTime+username,
                url: Http+this.clockCode+"&topic="+this.topic,
                responseType: 'json',
                headers: {
                    'content-type': 'application/json'
                },
                method: 'GET',
                success: res => {
                    console.info(res)
                    console.info("我是pinglun列表：" + res.data)
                    this.cancelSchedule()
                },
                fail:{
                }
            });
        }

    },
    changeMode(e) {
        this.modeFlag = e.mode
    }

}
